package com.wzl.qs;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.wzl.qs.db.DbAdapter;
import com.wzl.qs.db.ENote;

public class QuitSmoking extends ListActivity {

	Button btnTestInsert;
	DbAdapter dbAdapter;
	private static final int ABOUT = 1;
	private static final int HELP = 2;

	// private static final int ACTIVITY_CREATE = 0;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		dbAdapter = new DbAdapter(this);
		dbAdapter.open();

		// pop data, one time
//		popdata();

		// List the notes
		displayList();
	}

	private void displayList() {
		ArrayList<ENote> noteList = dbAdapter.GetAllENotes();
		NoteAdapter noteAdapter = new NoteAdapter(this, noteList);
		setListAdapter(noteAdapter);
	}

	// List item is clicked
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		ENote eNote = ((NoteAdapterView) v).getENote();

		Intent i = new Intent(this, NoteView.class);
		i.putExtra(DbAdapter.NID_FIELD, eNote.getNid());

		// Start activity to execute the intent
		startActivity(i);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuItem mi = menu.add(Menu.NONE, ABOUT, Menu.NONE, "About");
		mi.setIcon(R.drawable.qsm);
		MenuItem mihelp = menu.add(Menu.NONE, HELP, Menu.NONE, "Help");
		mihelp.setIcon(android.R.drawable.ic_dialog_info);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int itemid = item.getItemId();
		switch (itemid) {
		case ABOUT:
			Intent i = new Intent(this, About.class);
			startActivity(i);
			break;

		case HELP:
			Intent aboutIntent = new Intent(this, Help1.class);
			startActivity(aboutIntent);
			break;

		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onResume() {
		super.onResume();
		displayList();
	}

	private void popdata() {

		String[] photo_paths = { "/sdcard/want_you_quit_smoking.jpg",
				"/sdcard/break_cigar.jpg", "/sdcard/quit-smoking-shorter.jpg",
				"/sdcard/stopsmoking_stop_breathing.jpg",
				"/sdcard/quit-smoking-bomb.jpg",
				"/sdcard/quit-smoking-chimney.jpg"

		};
		String[] senders = { "Best friend", "Wife", "Xiaohui", "Wu Wei",
				"Xiaoping", "Mother" };
		String[] desc = {
				"I want you to quit smoking, Now.",
				"Darling, quite smoking today. OK?",
				"life is endangered from the moment when you start smoking. Quit today.",
				"To smoke or to breath.",
				"Many die of smoking every day in the world. Don't be one of them.",
				"Stop polluting your family. Quit smoking today" };
		int[] popu = { 24, 25, 21, 32, 20, 19 };
		String today = DateTimeUtils.getToday();
		for (int i = 0; i < photo_paths.length; i++) {
			ENote note = new ENote();
			note.setName(senders[i]);
			note.setPhotoPath(photo_paths[i]);
			note.setThumberNail(photo_paths[i]);
			note.setPopularity(popu[i]);
			note.setDescription(desc[i]);
			note.setInputDate(today);
			dbAdapter.AddENote(note);
		}

	}

}